﻿using Bentley.CifNET.Geometry;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK.Edit;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BCIMNETGM = Bentley.CIMNET.CIMGeometry.Mesh;

namespace CheccCoverplateCulvertAssemble.Mesh
{
    public class ElementProcess
    {
        public List<Element> GetModelElements()
        {
            List<Element> list = new List<Element>();
            using (IEnumerator<Element> enumerator2 = Session.Instance.GetActiveDgnModel().GetGraphicElements().GetEnumerator())
            {
                while (enumerator2.MoveNext())
                {
                    Element item = enumerator2.Current;
                    list.Add(item);
                }
            }
            return list;
        }

        public PolyfaceHeader GetElementInfo(Element element)
        {
            MSElementType elementType = element.ElementType;

            switch (elementType)
            {
                case MSElementType.CellHeader:
                {
                    return null;
                }
                case MSElementType.Solid:
                {
                    return CreateShapeRepresentation_Element(element);
                }
            }
            return null;
        }

        public static PolyfaceHeader CreateShapeRepresentation_Element(Element ele)
        {
            FacetOptions facetOptions = new FacetOptions();
            facetOptions.SetDefaults();
            facetOptions.SetCurveDefaultsDefaults();
            facetOptions.ConvexFacetsRequired = true;
            facetOptions.CurvedSurfaceMaxPerFace = 3;
            facetOptions.EdgeHiding = false;
            facetOptions.MaxPerFace = 3;
            facetOptions.MaxEdgeLength = 10000;
            facetOptions.NormalsRequired = true;
            PolyfaceHeader polyfaceHeader = null;
            BCIMNETGM.MeshSDKNET.CreateMeshFromElement(ref polyfaceHeader, ele, Session.Instance.GetActiveDgnModel());
            if (polyfaceHeader != null)
            {
                var outputPolyfaceHeaders = new List<PolyfaceHeader>();
                var meshProcessor = new MeshProcess(facetOptions, ref outputPolyfaceHeaders);
                ElementGraphicsOutput.Process(ele, meshProcessor);
                

                foreach (var outputPolyfaceHeader in outputPolyfaceHeaders)
                {
                    int a = outputPolyfaceHeader.Point.Count();
                    return outputPolyfaceHeader;
                    //var meshHeaderElement = new MeshHeaderElement(Session.Instance.GetActiveDgnModel(), null, outputPolyfaceHeader);
                    //if (!meshHeaderElement.IsPersistent) meshHeaderElement.AddToModel();
                }
            }
            return null;
        }
        public static PolyfaceHeader CreateShapeRepresentation_Polyface(PolyfaceHeader polyfaceHeader)
        {
            FacetOptions facetOptions = new FacetOptions();
            facetOptions.SetDefaults();
            facetOptions.SetCurveDefaultsDefaults();
            facetOptions.ConvexFacetsRequired = true;
            facetOptions.CurvedSurfaceMaxPerFace = 3;
            facetOptions.EdgeHiding = false;
            facetOptions.MaxPerFace = 3;
            facetOptions.MaxEdgeLength = 10000;
            facetOptions.NormalsRequired = true;
            if (polyfaceHeader != null)
            {
                var outputPolyfaceHeaders = new List<PolyfaceHeader>();
                var meshProcessor = new MeshProcess(facetOptions, ref outputPolyfaceHeaders);
                MeshHeaderElement meshEle = new MeshHeaderElement(Session.Instance.GetActiveDgnModel(),null, polyfaceHeader);
                ElementGraphicsOutput.Process(meshEle, meshProcessor);


                foreach (var outputPolyfaceHeader in outputPolyfaceHeaders)
                {
                    int a = outputPolyfaceHeader.Point.Count();
                    DTransform3d dTransform3D = DTransform3d.Scale(CheccCulvertHelpers.GeneralHelpers.UorToMeters);
                    //TransformInfo transInfo1 = new TransformInfo(DTransform3d.Scale(CheccCulvertHelpers.GeneralHelpers.UorToMeters));
                    outputPolyfaceHeader.Transform(ref dTransform3D,false);
                    //var meshHeaderElement = new MeshHeaderElement(Session.Instance.GetActiveDgnModel(), null, outputPolyfaceHeader);
                    //if (!meshHeaderElement.IsPersistent)
                    //{
                    //    meshHeaderElement.ApplyTransform(transInfo1);   
                    //    meshHeaderElement.AddToModel();
                    //} 
                    return outputPolyfaceHeader;
                }
            }
            return null;
        }
    }
}
